package cn.pxs.controller;

import cn.pxs.entity.*;
import cn.pxs.mapper.HotMapper;
import cn.pxs.service.*;
import cn.pxs.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/api/public")
public class PublicController {
    @Autowired
    private HotService hotService;
    @Autowired
    private UserService userService;
    @Autowired
    private BookService bookService;
    @Autowired
    private LargeFileService largeFileService;
    @Autowired
    private CartService cartService;
    @Autowired
    private BookTypeService bookTypeService;


    @RequestMapping("/findHot/{num}/{type}")
    @ResponseBody
    //地址栏绑定 num 和 type
    //热度查询
    public Map findHot(@PathVariable("num")Integer num,
                       @PathVariable("type")Integer type){
        List<Hot> hots = hotService.findByType(num, type);
        if(hots.size() == 0 ){
            return RespJson.pack(404,"小的找不到",hots);
        }
        return RespJson.pack(200,"你要的数据",hots);
    }

    @PostMapping("/book/findIf/{start}/{num}")
    @ResponseBody
    //地址栏 绑定 start 和 num
    //条件查询
    public Map findIf(@PathVariable("start")Integer start,
                      @PathVariable("num")Integer num,String key){
        //key 和 null 对换这里会报错
        if(("").equals(key) || null == key || key.length()<= 1){
            return RespJson.pack(100,"key值少了或不存在",null);
        }
        Map map = new HashMap();
        map.put("total",bookService.findByIfIDLine(key));
        map.put("books",bookService.findIf(start, num, key));
        return RespJson.pack(200,"查询成功",map);
    }

    @RequestMapping("/addUser")
    @ResponseBody
    //通过 JSON格式 上传 user对象
    //添加用户
    public Map addUser(@RequestBody User user){
        System.out.println(user);
        System.out.println("===");
//        System.out.println(book);
        try{
            userService.add(user);
        }catch (Exception e){
            return RespJson.pack(100,e.toString(),null);
        }
        return RespJson.pack(200,"添加成功",null);
    }


    @RequestMapping("/findBookByPage/{start}/{num}/{typeid}")
    @ResponseBody
    //通过 地址栏 绑定 start 和 num 和typeid
    //书本 分页查询
    public Map findBookByPage(@PathVariable("start")Integer start,
                               @PathVariable("num")Integer num,
                               @PathVariable("typeid")Integer typeid){
        Map map = new HashMap();
        map.put("total",bookService.findByTypeIDLine(typeid));
        if(typeid == 0){
            typeid = null;
            map.put("total",bookService.findByLine());
        }
        map.put("books",bookService.findBookByPage(start, num, typeid));
        return RespJson.pack(200,"成功.....",map);
    }

    @RequestMapping(value = "/showImg/{id}",produces = MediaType.IMAGE_GIF_VALUE)
    @ResponseBody
    // 地址栏 绑定 id
    //显示图片
    public byte[] showImg(@PathVariable("id")String picture){
        LargeFile file = largeFileService.findById(picture);
        Object o = file.getContent();
        byte[] buf = (byte[])o;
        return buf;
    }

    @RequestMapping("/addCart/{bookid}")
    @ResponseBody
    //地址栏绑定 bookid
    //添加购物车
    public Map addCart(@PathVariable("bookid")Integer bookid,Cart cart){
//        if(cart.getUid() == null) return RespJson.pack(401,"未登录",null);
        cart.setUid(25);
        if(cart.getUid() != null){
            cart.setBookid(bookid);
            //设个数值测试 可能要根据登录判断
            cart.setNum(1);
            cart.setTcreate(new Date(new java.util.Date().getTime()));
            cartService.add(cart);
        }
        return RespJson.pack(200,"添加成功",null);
    }

    @GetMapping("/type/findAll")
    @ResponseBody
    //书本类型92 全查询
    public Map findTypeAll(){
        return RespJson.pack(200,"查询成功",bookTypeService.findAll());
    }

    @GetMapping("/type/findAll2")
    @ResponseBody
    //书本类型93 全查询
    public Map findTypeAll2(){
        return RespJson.pack(200,"查询成功",bookTypeService.findAll2());
    }

    @RequestMapping("/book/addRate/{bookid}")
    @ResponseBody
    //点赞添加
    public Map addRate(@PathVariable("bookid")Integer id){
        bookService.updateRate(id);
        return RespJson.pack(200,"点赞加一",null);
    }

    @RequestMapping("/book/findRate5")
    @ResponseBody
    //查询点赞前五
    public Map findRate5(){
        return RespJson.pack(200,"成了",bookService.findRate5());
    }

    @RequestMapping("/book/findSale5")
    @ResponseBody
    //查询销售量前五
    public Map findSale5(){
        return RespJson.pack(200,"成了",bookService.findSale5());
    }

    @RequestMapping("/book/findFav5")
    @ResponseBody
    //查询收藏量前五
    public Map findFav5(){
        return RespJson.pack(200,"成了",bookService.findFav5());
    }

    @RequestMapping("/findBookByPageRate/{start}/{num}/{typeid}")
    @ResponseBody
    //地址栏绑定 start 和 num 、 typeid
    //查询书本点赞分页
    public Map findBookByPageRate(@PathVariable("start")Integer start,
                                  @PathVariable("num")Integer num,@PathVariable("typeid")Integer typeid){
        Map map = new HashMap();
        map.put("total",bookService.findByTypeIDLine(typeid));
        if(typeid == 0){
            typeid = null;
            map.put("total",bookService.findByLine());
        }
        map.put("books",bookService.findBookByPageRate(start, num, typeid));
        return RespJson.pack(200,"查询成功",map);
    }

    @RequestMapping("/findBookByPageSale/{start}/{num}/{typeid}")
    @ResponseBody
    //查询书本销售分页
    public Map findBookByPageSale(@PathVariable("start")Integer start,
                                  @PathVariable("num")Integer num,@PathVariable("typeid")Integer typeid){
        Map map = new HashMap();
        map.put("total",bookService.findByTypeIDLine(typeid));
        if(typeid == 0){
            typeid = null;
            map.put("total",bookService.findByLine());
        }
        map.put("books",bookService.findBookByPageSale(start, num, typeid));
        return RespJson.pack(200,"查询成功",map);
    }

    @RequestMapping("/findBookByPageFav/{start}/{num}/{typeid}")
    @ResponseBody
    //查询书本收藏 分页
    public Map findBookByPageFav(@PathVariable("start")Integer start,
                                 @PathVariable("num")Integer num,@PathVariable("typeid")Integer typeid){
        Map map = new HashMap();
        map.put("total",bookService.findByTypeIDLine(typeid));
        if(typeid == 0){
            typeid = null;
            map.put("total",bookService.findByLine());
        }
        map.put("books",bookService.findBookByPageFav(start, num, typeid));
        return RespJson.pack(200,"查询成功",map);
    }

}
